package com.slw.caiji.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slw.caiji.Vo.params.PageParams;
import com.slw.caiji.dto.SysUser;
import com.slw.caiji.mapper.SysUserMapper;
import com.slw.caiji.service.SysUserService;
import com.slw.caiji.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result getSysUserList(PageParams pageParams) {
        final Page<SysUser> objectPage = new Page<>(pageParams.getCurrent(), pageParams.getPageSize());
        final IPage<SysUser> sysUserIPage = sysUserMapper.listSysUser(objectPage);
        return Result.success(sysUserIPage);
    }

    @Override
    public Result addUser(SysUser sysUser) {
        final SysUser sysUser1 = new SysUser();
        sysUser1.setName(sysUser.getName());
        sysUser1.setNickName(sysUser.getNickName());
        sysUser1.setAvatar(sysUser.getAvatar());
        sysUser1.setPassword(sysUser.getPassword());
        sysUser1.setEmail(sysUser.getEmail());
        sysUser1.setMobile(sysUser.getMobile());
        return Result.success(sysUserMapper.insert(sysUser1));
    }

    @Override
    public Result delUser(String ids) {
        final String[] split = ids.split(",");
        for (String s : split) {
            sysUserMapper.deleteById(s);
        }
        return Result.success(1);
    }

    @Override
    public Result updateUser(SysUser sysUser) {
        SysUser sysUser1 = new SysUser();
        sysUser1.setName(sysUser.getName());
        sysUser1.setNickName(sysUser.getNickName());
        sysUser1.setAvatar(sysUser.getAvatar());
        sysUser1.setPassword(sysUser.getPassword());
        sysUser1.setEmail(sysUser.getEmail());
        sysUser1.setMobile(sysUser.getMobile());
        sysUser1.setId(sysUser.getId());
        return Result.success(sysUserMapper.updateById(sysUser1));
    }

    @Override
    public Result detailUser(String id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        return Result.success(sysUser);
    }
}
